
//
//  UserMode.m
//  DogeLegs
//
//  Created by 宋华 on 2017/8/17.
//  Copyright © 2017年 宋华. All rights reserved.
//

#import "UserMode.h"
#import <objc/runtime.h>

#define LoginName @"loginName"
#define USER_ID @"userId"
#define USERNAME @"userName"
#define PASSWord @"passWord"
#define LASTLognTime @"lastLoginTime"
#define TOKEN @"token"
#define CUSTREMARKNAMES @"custRemarkNames"
#define MSGCOUNT @"msgCount"
#define Amount @"amount"
#define EMAIL @"email"

@implementation UserMode

/*
 使用runtime进行解档与归档。
 */
-(void)encodeWithCoder:(NSCoder *)aCoder{
    unsigned int count = 0;
    Ivar *ivarLists = class_copyIvarList([UserMode class], &count);// 注意下面分析
    for (int i = 0; i < count; i++) {
        const char* name = ivar_getName(ivarLists[i]);
        NSString* strName = [NSString stringWithUTF8String:name];
        [aCoder encodeObject:[self valueForKey:strName] forKey:strName];
    }
    free(ivarLists);   //一定不要忘了，自己释放。
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super init]) {
        unsigned int count = 0;
        Ivar *ivarLists = class_copyIvarList([UserMode class], &count);
        for (int i = 0; i < count; i++) {
            const char* name = ivar_getName(ivarLists[i]);
            NSString* strName = [NSString stringWithCString:name encoding:NSUTF8StringEncoding];
            id value = [aDecoder decodeObjectForKey:strName];
            [self setValue:value forKey:strName];
        }
        free(ivarLists);
    }
    return self;
}
/*
- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self == [super init]) {
        self.userId = [aDecoder decodeObjectForKey:USER_ID];
        self.loginName = [aDecoder decodeObjectForKey:LoginName];
        self.email = [aDecoder decodeObjectForKey:EMAIL];
        self.userName = [aDecoder decodeObjectForKey:USERNAME];
        self.password = [aDecoder decodeObjectForKey:PASSWord];
        self.lastLoginTime = [aDecoder decodeObjectForKey:LASTLognTime];
        self.token = [aDecoder decodeObjectForKey:TOKEN];
        self.custRemarkNames = [aDecoder decodeObjectForKey:CUSTREMARKNAMES];
        self.msgCount = [aDecoder decodeObjectForKey:MSGCOUNT];
        self.amount = [aDecoder decodeObjectForKey:Amount];
    }
    
    return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:self.userId forKey:USER_ID];
    [aCoder encodeObject:self.loginName forKey:LoginName];
    [aCoder encodeObject:self.email forKey:EMAIL];
    [aCoder encodeObject:self.userName forKey:USERNAME];
    [aCoder encodeObject:self.password forKey:PASSWord];
    [aCoder encodeObject:self.lastLoginTime forKey:LASTLognTime];
    [aCoder encodeObject:self.token forKey:TOKEN];
    [aCoder encodeObject:self.custRemarkNames forKey:CUSTREMARKNAMES];
    [aCoder encodeObject:self.msgCount forKey:MSGCOUNT];
    [aCoder encodeObject:self.amount forKey:Amount];
}
*/
+ (UserMode *)userModeFromDict:(NSDictionary *)dict {
    if (![dict isKindOfClass:[NSDictionary class]]) {
        return nil;
    }
    UserMode *user = [[UserMode alloc] init];
    
 
    return user;
}


@end
